<?php
if(!defined('BR'))
	define('BR', "\r\n");


error_reporting(E_ALL);

define('FILE_APPEND', 1);
if(!function_exists("file_put_contents"))
{
	function file_put_contents($n, $d, $flag = false)
	{
		$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
		$f = @fopen($n, $mode);

		if($f === false)
		{
			return 0;
		}

		else
		{
			if(is_array($d))
				$d = implode($d);

			$bytes_written = fwrite($f, $d);
			fclose($f);
			return $bytes_written;
		}
	}
}


function get_files_by_ext($path, $ext)
{
	$files = array();

	if(is_dir($path))
	{
		$handle = opendir($path);

		while($file = readdir($handle))
		{
			if($file[0] == '.')
			{
				continue;
			}

			if($ext=='*'||is_file($path . $file) && preg_match('/\.' . $ext . '$/i', $file))
			{
				$files[] = $file;
			}
		}

		closedir($handle);
		sort($files);
	}

	return $files;
}

//chcp 65001
exec("chcp 65001");

//读取配置
$cfgFile='domain.ini';
$domain='';
$title='信智联';
if (@isset($argv[1]))
	$title=$argv[1];

if((@$domain = trim(file_get_contents($cfgFile))) == '')
{
	echo '未配置域名，无法继续生成...'.BR;
	exit(0);
}
//生成
$html_head='
<html> 
<head>
	<title>LookPages</title>
</head> 
<body><h1>'.$title.'</h1>

	';
	$html_foot='

</body> 
</html> 
';
$str ='';
foreach(get_files_by_ext('../file/', '*') as $file)
{
	$ahref=$domain.'/file/' .$file . '';
	;
	$str .= '<a href="https://view.officeapps.live.com/op/view.aspx?src=' .urlencode($ahref). '">'.$file.'</a><br>';

	if(!defined('UTF8'))
		echo BR . '找到: ' . $file . BR;
}

//保存主页

file_put_contents('../index.html', $html_head.$str.$html_foot);

?>